package com.free.bsf.eureka.client;

import com.free.bsf.core.config.BsfConfiguration;
import com.free.bsf.core.util.LogUtils;
import com.free.bsf.core.util.WebUtils;
import com.free.bsf.eureka.client.customip.FeignClientCustomIpAspect;
import com.free.bsf.eureka.client.filter.EurekaStatusFilter;
import com.free.bsf.eureka.client.format.DateFormatRegister;
import com.free.bsf.eureka.client.limitrate.LimitRateAop;
import com.free.bsf.eureka.client.limitrate.LimitRateProperties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.Ordered;

/**
 * @author: chejiangyi
 * @version: 2019-06-15 14:50
 **/
@Configuration
@ConditionalOnProperty(name = "bsf.eureka.client.enabled", havingValue = "true")
@Import(BsfConfiguration.class)
@EnableConfigurationProperties({EurekaProperties.class})
@EnableEurekaClient
public class EurekaClientConfiguration implements InitializingBean {
    @Autowired
    EurekaProperties eurekaProperties;

    @Override
    public void afterPropertiesSet() {
        LogUtils.info(EurekaClientConfiguration.class,EurekaProperties.ClientProject,"已启动!!!"+" "+EurekaProperties.EurekaClientServiceUrlDefaultZone+"="+ eurekaProperties.getDefaultZone());
    }
    @Bean
    @ConditionalOnClass(name = "com.free.bsf.health.collect.HealthCheckProvider")
    @Lazy
    public EurekaMonitor eurekaClientMonitor(){
        return new EurekaMonitor();

    }

    @Bean
    @ConditionalOnProperty(name = "bsf.feign.dateformat.enabled", havingValue = "true",matchIfMissing=true)
    public DateFormatRegister dateFormatRegister(){
        return new DateFormatRegister();
    }
    
    @Bean
    @ConditionalOnProperty(name ="bsf.eureka.limitrate.enabled", havingValue = "true",matchIfMissing=false)
    @ConditionalOnClass(name = "org.aspectj.lang.annotation.Aspect")
    public LimitRateAop limitRateControllerAspect(){
        return new LimitRateAop();
    }
    
    @ConditionalOnProperty(name ="bsf.eureka.limitrate.enabled", havingValue = "true",matchIfMissing=false)
    @Bean
    public LimitRateProperties limitRateProperties() {
        return new LimitRateProperties();
    }

    @Bean
    @ConditionalOnProperty(name ="bsf.feign.custom.enabled", havingValue = "true")
    @ConditionalOnClass(name = "org.aspectj.lang.annotation.Aspect")
    public FeignClientCustomIpAspect feignClientCustomIpAspect(){
        return new FeignClientCustomIpAspect();
    }

    @ConditionalOnProperty(name = "bsf.eureka.changeClientStatus.enabled", havingValue = "true", matchIfMissing = false)
    @Bean
    @ConditionalOnWebApplication
    public FilterRegistrationBean eurekaChangeClientStatusFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        // 优先注入
        filterRegistrationBean.setOrder(Ordered.LOWEST_PRECEDENCE);
        filterRegistrationBean.setFilter(new EurekaStatusFilter());
        filterRegistrationBean.setName("eurekaChangeClientStatusFilter");
        filterRegistrationBean.addUrlPatterns("/bsf/eureka/changeClientStatus/*");
        LogUtils.info(EurekaClientConfiguration.class, EurekaProperties.Project,
                "eureka changeClientStatus 注册成功,访问:" + WebUtils.getBaseUrl() + "/bsf/eureka/changeClientStatus/");
        return filterRegistrationBean;
    }
}
